package com.xiumu.service.blog.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiumu.common.core.enums.PostStatus;
import com.xiumu.common.core.exception.blog.BlogException;
import com.xiumu.common.core.page.PageQuery;
import com.xiumu.common.core.utils.AssertUtil;
import com.xiumu.dao.blog.BlogPostDao;
import com.xiumu.pojo.blog.dto.BlogPostDTO;
import com.xiumu.pojo.blog.entity.BlogPost;
import com.xiumu.pojo.blog.entity.BlogPostTag;
import com.xiumu.pojo.blog.query.BlogPostQuery;
import com.xiumu.pojo.blog.vo.BlogPostVO;
import com.xiumu.service.blog.BlogPostService;
import com.xiumu.service.blog.BlogPostTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文章信息 Service 接口实现
 *
 * @author XiuMu
 * @Date 2023-01-29 09:28:02
 */
@Service
public class BlogPostServiceImpl extends ServiceImpl<BlogPostDao, BlogPost> implements BlogPostService {

    @Autowired
    private BlogPostTagService blogPostTagService;

    @Override
    public Page<BlogPostVO> listPage(PageQuery<BlogPostQuery, BlogPost> pageQuery) {
        Page<BlogPost> postPage = this.baseMapper.selectPage(pageQuery.toPage(), pageQuery.getCondition());
        Page<BlogPostVO> voPage = new Page<>();
        BeanUtil.copyProperties(postPage, voPage);
        List<BlogPostVO> voList = BeanUtil.copyToList(postPage.getRecords(), BlogPostVO.class);
        if (CollUtil.isNotEmpty(voList)) {
            List<BlogPostTag> tags = blogPostTagService.listByPostId(voList.stream().map(BlogPostVO::getId).toList());
            Map<Long, List<BlogPostTag>> tagMap = tags.stream().collect(Collectors.groupingBy(BlogPostTag::getPostId));
            for (BlogPostVO blogPostVO : voList) {
                List<BlogPostTag> tagList = tagMap.get(blogPostVO.getId());
                if (CollUtil.isNotEmpty(tagList)) {
                    blogPostVO.setTagList(tagList.stream().map(BlogPostTag::getTagName).toList());
                }
            }
        }
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public List<BlogPost> listByBlogPost(BlogPostQuery blogPost) {
        return this.baseMapper.selectByBlogPost(blogPost);
    }

    @Override
    @Transactional
    public boolean create(BlogPostDTO blogPostDTO) {
        BlogPost blogPost = BeanUtil.copyProperties(blogPostDTO, BlogPost.class);
        // 保存草稿的时候，就直接保存
        if (blogPost.getPostStatus() == PostStatus.DRAFT) {
            return this.save(blogPost);
        } else {
            // 发布文章的时候对参数进行验证
            AssertUtil.isNotNull(blogPost.getCategoryId(), BlogException.CATEGORY_NOT_EXIST);
            AssertUtil.isTrue(StrUtil.isNotBlank(blogPost.getPostSummary()), BlogException.CATEGORY_NOT_EXIST);
            this.save(blogPost);
            // 绑定标签
            if (CollUtil.isNotEmpty(blogPostDTO.getTagList())) {
                List<BlogPostTag> postTagList = blogPostDTO.getTagList().stream().map(tag -> new BlogPostTag(blogPost.getId(), tag)).toList();
                blogPostTagService.saveBatch(postTagList);
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updateById(BlogPostDTO blogPostDTO, Long id) {
        BlogPost blogPost = getById(id);
        AssertUtil.isNotNull(blogPost, BlogException.NOT_EXIST);
        blogPost = BeanUtil.copyProperties(blogPostDTO, BlogPost.class);
        blogPost.setId(id);
        blogPostTagService.updatePostTag(id, blogPostDTO.getTagList());
        return updateById(blogPost);
    }

    @Override
    public BlogPostVO getBlogPostVOById(Long id) {
        BlogPost blogPost = getById(id);
        AssertUtil.isNotNull(blogPost, BlogException.NOT_EXIST);
        BlogPostVO blogPostVO = BeanUtil.copyProperties(blogPost, BlogPostVO.class);
        List<BlogPostTag> tags = blogPostTagService.listByPostId(id);
        if (CollUtil.isNotEmpty(tags)) {
            blogPostVO.setTagList(tags.stream().map(BlogPostTag::getTagName).toList());
        }
        return blogPostVO;
    }
}




